import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import {
  ApiOperation,
  ApiResponse,
  ApiBody,
  ApiParam,
  ApiTags,
} from '@nestjs/swagger';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { Role } from './entities/role.entity';
import { AssignPermissionsDto } from './dto/assign-permissions.dto';

@Controller('role')
@ApiTags('角色管理') // 为控制器添加标签
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  // 创建单个角色
  @Post()
  @ApiOperation({ summary: '创建角色' })
  @ApiBody({ type: CreateRoleDto, description: '创建角色的详细信息' })
  @ApiResponse({ status: 201, description: '角色创建成功', type: Role })
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @Post('assign-permissions')
  @ApiOperation({ summary: '为角色分配权限' })
  @ApiBody({
    type: AssignPermissionsDto,
    description: '为角色分配权限的详细信息',
  })
  @ApiResponse({ status: 200, description: '权限分配成功', type: Role })
  @ApiResponse({ status: 404, description: '角色或权限未找到' })
  async assignPermissions(@Body() assignPermissionsDto: AssignPermissionsDto) {
    return this.roleService.assignPermissions(assignPermissionsDto);
  }

  // 查询所有角色
  @Get()
  @ApiOperation({ summary: '获取所有角色' })
  @ApiResponse({ status: 200, description: '返回所有角色数据', type: [Role] })
  findAll() {
    return this.roleService.findAll();
  }

  @Post('import')
  @ApiOperation({ summary: '一键导入角色' })
  async importRoles() {
    return this.roleService.importRoles();
  }

  // 根据角色ID查询角色
  @Get(':id')
  @ApiOperation({ summary: '根据ID获取角色' })
  @ApiParam({ name: 'id', type: String, description: '角色ID' })
  @ApiResponse({ status: 200, description: '返回角色数据', type: Role })
  findOne(@Param('id') id: string) {
    return this.roleService.findOne(id);
  }

  // 更新角色
  @Patch(':id')
  @ApiOperation({ summary: '更新角色' })
  @ApiParam({ name: 'id', type: String, description: '角色ID' })
  @ApiBody({ type: UpdateRoleDto, description: '更新角色的详细信息' })
  @ApiResponse({ status: 200, description: '角色更新成功', type: Role })
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(id, updateRoleDto);
  }

  // 删除角色
  @Delete(':id')
  @ApiOperation({ summary: '删除角色' })
  @ApiParam({ name: 'id', type: String, description: '角色ID' })
  @ApiResponse({ status: 200, description: '角色删除成功' })
  remove(@Param('id') id: string) {
    return this.roleService.remove(id);
  }

  // 批量添加角色
  @Post('bulk-create')
  @ApiOperation({ summary: '批量添加角色' })
  @ApiBody({
    type: [CreateRoleDto],
    description: '批量创建角色的详细信息',
  })
  @ApiResponse({
    status: 201,
    description: '角色批量创建成功',
    type: [Role],
  })
  async bulkCreate(@Body() createRoleDtos: CreateRoleDto[]) {
    return await this.roleService.createBulk(createRoleDtos);
  }

  // 批量删除角色
  @Delete('bulk-delete')
  @ApiOperation({ summary: '批量删除角色' })
  @ApiBody({
    type: [String],
    description: '批量删除角色ID列表',
  })
  @ApiResponse({
    status: 200,
    description: '角色批量删除成功',
  })
  async bulkDelete(@Body() ids: string[]) {
    return await this.roleService.removeBulk(ids);
  }
}
